<template>
  <div class="llm-answer">
    <h1>大模型回答管理</h1>
    
    <!-- 查询区域 -->
    <div class="search-area">
      <div class="search-inputs">
        <input v-model="modelName" placeholder="输入模型名称" />
        <input v-model="questionId" placeholder="输入问题ID" />
        <button @click="handleSearch()">查询</button>
      </div>
    </div>

    <!-- 结果表格 -->
    <div class="results-table">
      <table v-if="searchResults.length > 0">
        <thead>
          <tr>
            <th>使用模型</th>
            <th>标准问题ID</th>
            <th>回答内容</th>
            <th>更新时间</th>
          </tr>
        </thead>
        <tbody>
          <tr 
            v-for="item in searchResults" 
            :key="item.id"
          >
            <td>{{ item.model }}</td>
            <td>{{ item.question.id }}</td>
            <td @click="showAnswerDialog(item)" class="answer-cell">
              {{ truncateAnswer(item.answer) }}
            </td>
            <td>{{ formatDate(item.time) }}</td>
          </tr>
        </tbody>
      </table>
      <div v-else class="no-results">
        <p>暂无结果</p>
      </div>
    </div>
  </div>

  <!-- 回答详情浮窗 -->
  <div v-if="dialogVisible" class="answer-floating-window">
    <div class="floating-header">
      <h3>回答详情</h3>
      <button class="close-btn" @click="dialogVisible = false">×</button>
    </div>
    <div class="floating-content">
      <pre>{{ currentAnswer }}</pre>
    </div>
  </div>
  <div v-if="dialogVisible" class="floating-overlay" @click="dialogVisible = false"></div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { getLlmAnswers,type LlmAnswer } from '@/api/answer'

export default defineComponent({
  name: 'LLMAnswer',
  setup() {
    const modelName = ref('')
    const questionId = ref('')
    const searchResults = ref<LlmAnswer[]>([])
    const dialogVisible = ref(false)
    const currentAnswer = ref('')

    // 显示回答弹窗
    const showAnswerDialog = (item: LlmAnswer) => {
      currentAnswer.value = item.answer
      dialogVisible.value = true
    }

    // 截断回答内容
    const truncateAnswer = (answer: string) => {
      return answer.length > 20 ? answer.substring(0, 20) + '...' : answer
    }

    // 处理查询
    const handleSearch = async () => {
      try {
        const id = questionId.value ? parseInt(questionId.value) : undefined
        const model= modelName.value?modelName.value.toString() : undefined
        console.log('查询参数:', { model, id })
        const results = await getLlmAnswers(model, id)
        searchResults.value = results
      } catch (error) {
        console.error('查询失败:', error)
        searchResults.value = []
      }
    }

    // 格式化日期
    const formatDate = (dateStr: string) => {
      return new Date(dateStr).toLocaleString()
    }

    return {
      modelName,
      questionId,
      searchResults,
      handleSearch,
      formatDate,
      truncateAnswer,
      showAnswerDialog,
      dialogVisible,
      currentAnswer
    }
  }
})
</script>

<style scoped>
.llm-answer {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.search-area {
  margin: 20px 0;
}

.search-inputs {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.search-inputs input {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  flex: 1;
}

.search-inputs button {
  padding: 8px 16px;
  background-color: #2c3e50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.search-inputs button:hover {
  background-color: #1a252f;
}

.results-table {
  margin: 20px 0;
  overflow-x: auto;
}

.no-results {
  color: #666;
  font-size: 16px;
  text-align: center;
  padding: 20px;
}

.results-table table {
  width: 100%;
  border-collapse: collapse;
}

.results-table th,
.results-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

.results-table th {
  background-color: #f5f5f5;
  font-weight: bold;
}

.results-table tr:hover {
  background-color: #f9f9f9;
}

.answer-cell {
  cursor: pointer;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.answer-cell:hover {
  color: #315efb;
  text-decoration: underline;
}

.answer-floating-window {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60%;
  max-width: 800px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  z-index: 2000;
  animation: fadeIn 0.3s ease-out;
}

.floating-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.floating-header h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0 5px;
}

.close-btn:hover {
  color: #333;
}

.floating-content {
  max-height: 70vh;
  overflow-y: auto;
  padding: 20px;
}

.floating-content pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: inherit;
  margin: 0;
  line-height: 1.6;
}

.floating-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1999;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translate(-50%, -40%);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
}
</style>